package com.winsdom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.winsdom.domain.dto.HandsDto;
import com.winsdom.entity.Bills;
import com.winsdom.entity.ClaimRecord;
import com.winsdom.entity.PaymentReceipt;
import com.winsdom.entity.WriteOffRecord;
import com.winsdom.mapper.BillsMapper;
import com.winsdom.mapper.ClaimRecordMapper;
import com.winsdom.mapper.PaymentReceiptMapper;
import com.winsdom.mapper.WriteOffRecordMapper;
import com.winsdom.service.IWriteOffRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 核销记录表 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-05-22
 */
@Service
public class WriteOffRecordServiceImpl extends ServiceImpl<WriteOffRecordMapper, WriteOffRecord> implements IWriteOffRecordService {
     @Autowired
     private ClaimRecordMapper claimRecordMapper;
     @Autowired
     private PaymentReceiptMapper paymentReceiptMapper;
     @Autowired
     private BillsMapper billMapper;
     @Autowired
     private WriteOffRecordMapper writeOffRecordMapper;
    @Override
    public boolean hands(HandsDto handsDto) {

//        找收款
        List<PaymentReceipt> paymentReceipts=new ArrayList<>();
      for (int i = 0; i < handsDto.getHands().size(); i++) {
            LambdaQueryWrapper<PaymentReceipt> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PaymentReceipt::getPaymentId, handsDto.getHands().get(i));
            paymentReceipts.add(paymentReceiptMapper.selectOne(queryWrapper));
      }

          //遍历可以扣取金额
        BigDecimal a=new BigDecimal(0);
        for (int i = 0; i < paymentReceipts.size(); i++) {
            a=a.add(paymentReceipts.get(i).getReceiptAmount().subtract(paymentReceipts.get(i).getSettledAmount()));
        }

//        与核销金额对比 如果核销金额大于那么返回false
        if(a.compareTo(handsDto.getMonker())<0)
        {
            return false;
        }
//        从账单中扣除核销金额，从收款记录中扣除对应的净额


//        找出账单
        LambdaQueryWrapper<Bills> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bills::getId, handsDto.getId());
        Bills bills = billMapper.selectOne(queryWrapper);
//       减去核销的金额
        if(bills.getArrearsAmount().compareTo(handsDto.getMonker())<0)
        {
            return false;
        }
//        账单减去核销的金额
//        设置状态 如果核销金额大于账单的应收金额 那么账单状态为已结清
        Boolean z;
        if(z=(bills.getArrearsAmount().subtract(handsDto.getMonker())).compareTo(BigDecimal.ZERO)>0)
        {
            bills.setArrearsStatus(1);
        }
        else{
            bills.setArrearsStatus(2);
        }

//        减去核销的金额
        System.out.println(bills.getArrearsAmount());
        bills.setArrearsAmount(bills.getArrearsAmount().subtract(handsDto.getMonker()));

//        更改缴费记录
        for (int i = 0; i < paymentReceipts.size(); i++) {
            if(handsDto.getMonker().compareTo(BigDecimal.ZERO)==0)
            {
                break;
            }
//            金额够
            if(paymentReceipts.get(i).getReceiptAmount().subtract(paymentReceipts.get(i).getSettledAmount()).compareTo(handsDto.getMonker())>=0)
            {
//                直接核销
                paymentReceipts.get(i).setSettledAmount(paymentReceipts.get(i).getSettledAmount().add(handsDto.getMonker()));
                //                插入核销记录
                WriteOffRecord writeOffRecord=new WriteOffRecord();
                writeOffRecord.setPaymentId(paymentReceipts.get(i).getPaymentId());
                writeOffRecord.setBillsId(bills.getId());
                writeOffRecord.setAmount(handsDto.getMonker());
                writeOffRecord.setWriteOffDate(LocalDateTime.now());
                writeOffRecord.setStatus(1);
                writeOffRecord.setCreateTime(LocalDateTime.now());
                writeOffRecord.setUpdateTime(LocalDateTime.now());
                writeOffRecordMapper.insert(writeOffRecord);
//                插入数据
                handsDto.setMonker(new BigDecimal(0));



            }else {
//                不够核销一部分
                paymentReceipts.get(i).setSettledAmount(handsDto.getMonker().add(paymentReceipts.get(i).getSettledAmount()));
                WriteOffRecord writeOffRecord1=new WriteOffRecord();
                writeOffRecord1.setPaymentId(paymentReceipts.get(i).getPaymentId());
                writeOffRecord1.setBillsId(bills.getId());
//                设置核销记录的钱，等于
                writeOffRecord1.setAmount(paymentReceipts.get(i).getReceiptAmount().subtract(paymentReceipts.get(i).getSettledAmount()));
                writeOffRecord1.setWriteOffDate(LocalDateTime.now());
                writeOffRecord1.setStatus(1);
                writeOffRecord1.setCreateTime(LocalDateTime.now());
                writeOffRecord1.setUpdateTime(LocalDateTime.now());
                writeOffRecordMapper.insert(writeOffRecord1);
                handsDto.setMonker(handsDto.getMonker().subtract(paymentReceipts.get(i).getReceiptAmount().subtract(paymentReceipts.get(i).getSettledAmount())));
            }
        }
//       更新收费记录
         for(int i=0;i<paymentReceipts.size();i++)
         {
             LambdaQueryWrapper<PaymentReceipt> queryWrapper1 = new LambdaQueryWrapper<>();
             queryWrapper1.eq(PaymentReceipt::getPaymentId, paymentReceipts.get(i).getPaymentId());
             paymentReceiptMapper.update(paymentReceipts.get(i), queryWrapper1);
         }
//        更新账单
        billMapper.update(bills, new LambdaUpdateWrapper<Bills>().eq(Bills::getId, bills.getId()));
//         插入核销记录


        return true;


    }
}
